package si.unimb.src.wifisms.communicator;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import org.xmlpull.v1.XmlPullParserException;

import si.unimb.src.wifisms.communicator.types.ArrayOfString;
import si.unimb.src.wifisms.util.Utility;

/**
 * 
 * @author Ales
 * 
 * Proxy razred, ki izvede klice do Mobidesktop spletne storitve.
 *
 */
public class MobidesktopWS {

	// spremenljivke, potrebne za povezavo na Mobidesktop webservice
	private static final String SOAP_ACTION_PART = "http://mobitel.si/MobiDesktop/";
    private static final String NAMESPACE = "http://mobitel.si/MobiDesktop";
    
    private static HttpTransportSE androidHttpTransport = new HttpTransportSE("https://moj.mobitel.si/mobidesktop-v2/service");
    
    // Testni webservice
    //private static HttpTransportSE androidHttpTransport = new HttpTransportSE("http://10.0.2.2:8080/MDFService/MDFServiceSoap");

    /*
     * 
     * PUBLIC metode - 1:1 preslikava z dejanskim WS
     * 
     */

    /**
     * 
     * Metoda s klicem na Mobitelov WS poslje SMS sporocilo glede na vhodne podatke
     * 
     * @param username Uporabnisko ime (telefonska stevilka)
     * @param password Geslo (enako kot za www.planet.si)
     * @param phoneNumbers Seznam telefonskih stevilk, kamor se naj poslje SMS
     * @param message Vsebina SMS sporocila
     * @return Ali je bilo posiljanje uspesno?
     * @throws IOException
     * @throws XmlPullParserException
     * @throws SoapFault
     */
	public static boolean SendSMS(String username, String password, ArrayList<String> phoneNumbers, String message) throws IOException, XmlPullParserException, SoapFault {
	
		// Katera metoda v WS se klice
		final String METHOD_NAME = "SendSMS";

		// Dobimo in napolnimo request XML
		SoapObject request = getSoapObject(METHOD_NAME);

		request.addProperty("Username", username);
		request.addProperty("Password", password);

		phoneNumbers = Utility.cleanNumbers(phoneNumbers); // cleaning numbers

		ArrayOfString r = new ArrayOfString(phoneNumbers);
		
		PropertyInfo pi = new PropertyInfo();
        pi.setName("Recipients");
        pi.setValue(r);
        pi.setType(r.getClass());
        request.addProperty(pi);

		request.addProperty("Message", message);
		
		// Naredimo in posljemo v kuverti
		SoapSerializationEnvelope envelope = getEnvelope(request);
		SoapPrimitive result = (SoapPrimitive) sendEnvelope(getSoapAction(METHOD_NAME), envelope);

		// Vrnemo rezultat
		return Boolean.parseBoolean(result.toString());

	}
	
	/**
	 * 
	 * Metoda s klicem na Mobitelov WS preveri veljavnost prijavnih podatkov
	 * 
	 * @param username Uporabnisko ime (telefonska stevilka)
	 * @param password Geslo (enako kot za www.planet.si)
	 * @return Ali so prijavni podatki pravilni?
	 * @throws IOException
	 * @throws XmlPullParserException
	 * @throws SoapFault
	 * 
	 */
	public static boolean AuthenticateUser(String username, String password) throws IOException, XmlPullParserException, SoapFault {
		
		// Katera metoda v WS se klice
		final String METHOD_NAME = "AuthenticateUser";

		// Dobimo in napolnimo request XML
		SoapObject request = getSoapObject(METHOD_NAME);

		request.addProperty("Username", username);
		request.addProperty("Password", password);
		
		// Naredimo in posljemo v kuverti
		SoapSerializationEnvelope envelope = getEnvelope(request);
		SoapPrimitive result = (SoapPrimitive) sendEnvelope(getSoapAction(METHOD_NAME), envelope);

		// Dobimo rezultat
		int resultInt = Integer.parseInt(result.toString());
		
		//    0 = ok
		// -100 = narobe username/password
		if(resultInt == 0) {
		
			return true;
			
		} else {
		
			return false;
			
		}
	
	}
	
	/*
     * 
     * PRIVATE metode - za zdruzevanje skupne kode
     * 
     */
	private static String getSoapAction(String methodName) {
	    
    	return SOAP_ACTION_PART + methodName;
    	
    }
    
    private static SoapObject getSoapObject(String methodName) {
    	
    	return new SoapObject(NAMESPACE, methodName);
    	
    }
	
	private static SoapSerializationEnvelope getEnvelope(SoapObject request) {
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		return envelope;
		
	}
	
	private static Object sendEnvelope(String soapAction, SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException, SoapFault {
		
		androidHttpTransport.call(soapAction, envelope);
		return envelope.getResponse();
		
	}
	
}
